// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
    let message = cloud.getWXContext()

    let count_seek = await db.collection('card').where(
        {
            type: "seek",
            cardNum: event.cardNum,

        }
    ).count()
    let count_have = await db.collection('card').where(
        {
            type: "have",
            cardNum: event.cardNum,

        }
    ).count()


    if (count_seek.total != 0) {//有找饭卡的记录
        if (event.type == "have") {  //返回手机号码

            let mes = await db.collection('card').where(
                {
                    cardNum: event.cardNum,
                    type: "seek",
                }
            ).get()

            await cloud.openapi.subscribeMessage.send({
                touser: mes.data[0]._openid,
                data: {
                    'number2': {
                        value: event.cardNum
                    },
                    'phone_number3': {
                        value: event.phone
                    },
                    'thing5': {
                        value: '上面是失主的联系方式，赶紧归还饭卡吧！'
                    }
                },
                templateId: 'CmkR8dKf2u7N9kuPhSuhRr80Go4gOUU3evzk3XUQBG8',
                miniprogramState: 'developer'
            })

            return mes.data[0].phone
        } else {
            return "repeat"
        }
    } else if (count_have.total != 0) {//有捡到饭卡的记录
        if (event.type == "seek") {  //返回手机号码
            let mes = await db.collection('card').where(
                {
                    cardNum: event.cardNum,
                    type: "have",

                }
            ).get()

            await cloud.openapi.subscribeMessage.send({
                touser: mes.data[0]._openid,
                data: {
                    'number2': {
                        value: event.cardNum
                    },
                    'phone_number3': {
                        value: event.phone
                    },
                    'thing5': {
                        value: '上面是好心人的联系方式，赶紧联系对方吧！'
                    }
                },
                templateId: 'CmkR8dKf2u7N9kuPhSuhRr80Go4gOUU3evzk3XUQBG8',
                miniprogramState: 'developer'
            })
            
            return mes.data[0].phone
        } else {
            return "repeat"
        }
    } else { //新数据
        let data = {
            _openid: message.OPENID,
            type: event.type,
            phone: event.phone,
            cardNum: event.cardNum
        }

        let card = await db.collection('card').add({
            // data 字段表示需新增的 JSON 数据
            data: data
        })
        data._id = card._id
        await db.collection('gods').where({
            _openid: message.OPENID,
        }).update({
            data: {
                'publish.card': _.push(data),
                btList: _.push({
                    name: "发布饭卡丢失/捡到饭卡信息",
                    time: new Date(),
                    num: 5
                })
            }
        })
        return "ok"
    }

}